<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  
  <link rel="shortcut icon" href="../../img/favicon.ico">
  <title>Network Instantiation - Forge Networking Remastered</title>
  <link href='https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700' rel='stylesheet' type='text/css'>

  <link rel="stylesheet" href="../../css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../css/theme_extra.css" type="text/css" />
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css">
  
  <script>
    // Current page data
    var mkdocs_page_name = "Network Instantiation";
    var mkdocs_page_input_path = "UnityIntegration/network-instantiation.md";
    var mkdocs_page_url = null;
  </script>
  
  <script src="../../js/jquery-2.1.1.min.js" defer></script>
  <script src="../../js/modernizr-2.8.3.min.js" defer></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script> 
  
</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
      <div class="wy-side-nav-search">
        <a href="../.." class="icon icon-home"> Forge Networking Remastered</a>
        <div role="search">
  <form id ="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" title="Type search term here" />
  </form>
</div>
      </div>

      <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
	<ul class="current">
	  
          
            <li class="toctree-l1">
		
    <span class="caption-text">Home</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../..">User Manual (Forge Networking Remastered)</a>
                </li>
                <li class="">
                    
    <a class="" href="../../authoritative-design/">Authoritative Design</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Getting Started</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../GettingStarted/getting-started/">Getting Started</a>
                </li>
                <li class="">
                    
    <a class="" href="../../GettingStarted/basic-moving-cube-example/">Basic Moving Cube Example</a>
                </li>
                <li class="">
                    
    <a class="" href="../../GettingStarted/basic-rpc-example/">Basic RPC Example</a>
                </li>
                <li class="">
                    
    <a class="" href="../../GettingStarted/basic-instantiation-example/">Basic Instantiation Example</a>
                </li>
                <li class="">
                    
    <a class="" href="../../GettingStarted/jump-start-guide/">Jump Start Guide</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Network Contract Wizard (NCW)</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../NetworkContractWizard/network-contract-wizard-ncw/">Network Contract Wizard (NCW)</a>
                </li>
                <li class="">
                    
    <a class="" href="../../NetworkContractWizard/extending-generated-classes/">Extending Generated Classes</a>
                </li>
                <li class="">
                    
    <a class="" href="../../NetworkContractWizard/name-collision-issues/">Name Collision Issues</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Network Object</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../NetworkObject/changing-ownership/">Changing Ownership</a>
                </li>
                <li class="">
                    
    <a class="" href="../../NetworkObject/destroying-the-network-object/">Destroying the Network Object</a>
                </li>
                <li class="">
                    
    <a class="" href="../../NetworkObject/fields/">Fields</a>
                </li>
                <li class="">
                    
    <span class="caption-text">Remote Procedure Calls (RPCs)</span>
    <ul class="subnav">
                <li class="toctree-l3">
                    
    <a class="" href="../../NetworkObject/RemoteProcedureCalls/remote-procedure-calls/">Remote Procedure Calls</a>
                </li>
                <li class="toctree-l3">
                    
    <a class="" href="../../NetworkObject/RemoteProcedureCalls/buffered-rpcs/">Buffered RPCs</a>
                </li>
                <li class="toctree-l3">
                    
    <a class="" href="../../NetworkObject/RemoteProcedureCalls/sending-rpc-to-a-single-player/">Sending RPC to a Single Player</a>
                </li>
                <li class="toctree-l3">
                    
    <a class="" href="../../NetworkObject/RemoteProcedureCalls/rpcargs-and-rpcinfo-structs/">RpcArgs and RpcInfo Structs</a>
                </li>
                <li class="toctree-l3">
                    
    <a class="" href="../../NetworkObject/RemoteProcedureCalls/rpc-validation-by-server/">RPC Validation by Server</a>
                </li>
                <li class="toctree-l3">
                    
    <a class="" href="../../NetworkObject/RemoteProcedureCalls/replacing-previous-buffered-rpcs/">Replacing Previous Buffered RPCs</a>
                </li>
                <li class="toctree-l3">
                    
    <a class="" href="../../NetworkObject/RemoteProcedureCalls/clearing-buffered-rpcs/">Clearing Buffered RPCs</a>
                </li>
    </ul>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Unity Integration</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../gameobject-execution-order/">GameObject Execution Order</a>
                </li>
                <li class=" current">
                    
    <a class="current" href="./">Network Instantiation</a>
    <ul class="subnav">
            
    <li class="toctree-l3"><a href="#network-instantiation">Network Instantiation</a></li>
    
        <ul>
        
            <li><a class="toctree-l4" href="#getting-the-gameobject-from-instantiate">Getting the GameObject From Instantiate</a></li>
        
            <li><a class="toctree-l4" href="#notes">Notes</a></li>
        
            <li><a class="toctree-l4" href="#warnings">Warnings</a></li>
        
        </ul>
    

    </ul>
                </li>
                <li class="">
                    
    <a class="" href="../threading-in-unity/">Threading in Unity</a>
                </li>
                <li class="">
                    
    <a class="" href="../main-threading-rpcs/">Main Threading RPCs</a>
                </li>
                <li class="">
                    
    <a class="" href="../running-unity-specific-code-on-the-main-thread/">Running Unity specific code on the main thread</a>
                </li>
                <li class="">
                    
    <a class="" href="../network-start/">Unity Integration Network Start</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Basic Network Samples</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../BasicNetworkSamples/server-hosting-code/">Server Hosting Code</a>
                </li>
                <li class="">
                    
    <a class="" href="../../BasicNetworkSamples/client-connecting-code/">Client Connecting Code</a>
                </li>
                <li class="">
                    
    <a class="" href="../../BasicNetworkSamples/server-disconnect-client-code/">Server Disconnect Client Code</a>
                </li>
                <li class="">
                    
    <a class="" href="../../BasicNetworkSamples/ban-client-code/">Ban Client Code</a>
                </li>
                <li class="">
                    
    <a class="" href="../../BasicNetworkSamples/send-binary-frame/">Send Binary Frame</a>
                </li>
                <li class="">
                    
    <a class="" href="../../BasicNetworkSamples/send-file/">Send File</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Scene Navigation</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../SceneNavigation/loading-scenes/">Loading Scenes</a>
                </li>
                <li class="">
                    
    <a class="" href="../../SceneNavigation/scene-events/">Scene Events</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">NetWorker</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../NetWorker/temporarily-blocking-connections/">Temporarily Blocking Connections</a>
                </li>
                <li class="">
                    
    <a class="" href="../../NetWorker/thread-safe-player-iteration/">Thread Safe Player Iteration</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Master Server</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../MasterServer/quick-start/">Master Server Quick Start</a>
                </li>
                <li class="">
                    
    <a class="" href="../../MasterServer/connecting-to-master-server/">Connecting to Master Server</a>
                </li>
                <li class="">
                    
    <a class="" href="../../MasterServer/getting-host-list/">Getting Host List From Master Server</a>
                </li>
                <li class="">
                    
    <a class="" href="../../MasterServer/windows-server-firewall-setup/">Windows Server Firewall Setup</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Web Server</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../WebServer/jumpstart/">Web Server Jumpstart</a>
                </li>
                <li class="">
                    
    <a class="" href="../../WebServer/extending-with-mvc/">Web Server Extending With MVC</a>
                </li>
                <li class="">
                    
    <a class="" href="../../WebServer/command-plugins/">Web Server Command Plugins</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Troubleshooting</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../Troubleshooting/troubleshooting/">Troubleshooting</a>
                </li>
                <li class="">
                    
    <a class="" href="../../Troubleshooting/throttling-network-internal/">Throttling Network Internal</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../connection-cycle-events/">Events</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../rewinding/">Rewinding</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../network-logging/">Debugging</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../working-with-multiple-sockets/">Working With Multiple Sockets</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../nat-hole-punching/">NAT Hole Punching</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../lan-discovery/">LAN Discovery</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../offline-mode/">Offline Mode</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../lobby/">Lobby System</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../adding-and-updating-docs/">Contributing</a>
	    </li>
          
        </ul>
      </div>
      &nbsp;
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href="../..">Forge Networking Remastered</a>
      </nav>

      
      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href="../..">Docs</a> &raquo;</li>
    
      
        
          <li>Unity Integration &raquo;</li>
        
      
    
    <li>Network Instantiation</li>
    <li class="wy-breadcrumbs-aside">
      
    </li>
  </ul>
  <hr/>
</div>
          <div role="main">
            <div class="section">
              
                <h1 id="network-instantiation">Network Instantiation</h1>
<p>Network instantiation is how you are able to create objects on the network so that all other clients are able to see that object. Then you can update this object or send <a href="/GettingStarted/basic-rpc-example.md">Remote Procedure Calls</a> to the object to perform behaviors across the network or manage local behaviors.</p>
<p>When you use the <a href="/NetworkContractWizard/network-contract-wizard-ncw.md">Network Contract Wizard</a> you are able to generate what is known as a "Network Object". These objects can then be extended to have custom behavior and to implement network behaviors defined in the Network Contract Wizard (NCW). When you click the "Save &amp; Compile" option in the NCW, an instantiate method will be created for the object you just defined in the NCW. So if you created a Network Object in the NCW named <strong>SuperBall</strong> then you could call the following code in Unity to create an instance of that <strong>SuperBall</strong>:</p>
<pre><code class="csharp">NetworkManager.Instance.InstantiateSuperBall();
</code></pre>

<p>If you look at the input parameters of this method you will find that there are a few optional parameters:</p>
<pre><code>1. int index = 0
2. Vector3? position = null
3. Quaternion? rotation = null
4. bool sendTransform = true
</code></pre>

<ol>
<li>The index tells the network which prefab to instantiate (from the array set in the Network Manager prefab).</li>
<li>This is the position you wish to spawn the prefab at on the network. If left null then the object will spawn at the coordinates set in the prefab.</li>
<li>This is the rotation you wish to spawn the prefab at on the network. If left null then the object will spawn at the rotation set in the prefab.</li>
<li>This is used to know if the transform that is being assigned should be sent across the network. So if you filled out #2 and #3 then those values will be sent across the network and not just used locally. If you pass <code>false</code> then the prefab will spawn on the caller at the specified location, but it will be spawned at the prefab location on the other clients.</li>
</ol>
<p>You may recall from the above #1 that you can set various prefabs to spawn for a given network object. That is to say that my <strong>SuperBall</strong> could have more than one prefab it spawns on, even a cube (I know crazy right?). To work with this you will need to locate the <strong>NetworkManager</strong> prefab in the <code>Bearded Man Studios Inc/Prefabs</code> folder. Once you select this prefab, you will notice that there is a <code>NetworkPrefab</code> component attached to it with a <strong>Super Ball Network Object</strong> array that is empty. This is where you will drag and drop the various prefabs you wish to use as this type of network object. So we can drag a sphere and even a cube into this array.</p>
<p>Let's say that we first put a sphere into the array and then we put a cube into the array. You will notice that the sphere has an index of <strong>0</strong> and the cube will have an index of <strong>1</strong>. This is the value that you will supply to the instantiate method when you call it. If you were to pass <strong>0</strong>, you would get a sphere and if you were to pass a <strong>1</strong> you would get a cube.</p>
<h2 id="getting-the-gameobject-from-instantiate">Getting the GameObject From Instantiate</h2>
<p>What you may notice is that when you instantiate the <strong>SuperBall</strong> it will return a <strong>NetworkBehavior</strong> which will be your <strong>SuperBallBehavior</strong> that was generated. This object actually derives from the <code>MonoBehaviour</code> so you can directly access the gameObject, transform, and any standard <code>MonoBehaviour</code> field, property, and method as you would do normally.</p>
<pre><code class="csharp">var ball = NetworkManager.Instance.InstantiateSuperBall();

// Access any standard MonoBehaviour elements as seen below from the ball reference
Debug.Log(ball.gameObject.name);
Debug.Log(ball.transform.position);
</code></pre>

<hr />
<h2 id="notes">Notes</h2>
<p>So you may be thinking, well gee, a numbered index might be a little bit hard to keep track of, and if you do, then you are probably correct. Like all things software there are many ways that you can setup your project, but here are just 2 ideas that you can use to have a little better time managing the various prefabs you may use.</p>
<h3 id="method-1">Method 1</h3>
<p>You can create a class that is dedicated to mapping objects to strings or integers. Then you can access the <code>NetworkManager.Instance</code> and assign the array directly with <code>NetworkManager.Instance.SuperBallNetworkObject = myArray;</code>. By doing this you can create your own indexing and lookup logic for the prefabs that you can use to easily find the index for the prefab that should spawn on the network. After creating the mapping you just need to call your lookup method and have it do the rest of the work.</p>
<h3 id="method-2">Method 2</h3>
<p>You can create your own instantiate methods. You can wrap code around our code to spawn your objects. So imagine that you want to call the generated <strong>NetworkManager.Instance.InstantiateSuperBall</strong> method but you have some other stuff you always do before hand or that dynamically selects the proper index. In this case you can create a class that does all the helper logic before you call the instantiate method, then you would call the instantiate through this helper class.</p>
<h3 id="manual-attachment-method">Manual Attachment Method</h3>
<p>You can create a NetworkObject without having to call this instantiate method which is directly linked to a prefab. This obviously isn't a topic for this section but you can find out more about this in the <strong>Getting Started</strong> examples about network instantiation.</p>
<h2 id="warnings">Warnings</h2>
<p>If you are dynamically setting the array on the <strong>NetworkManager.Instance</strong> then you <strong>MUST</strong> make sure that the order being assigned is predictable on all clients. You can't exactly have the order of the array different on each client and expect it to still work properly can you?</p>
              
            </div>
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="../threading-in-unity/" class="btn btn-neutral float-right" title="Threading in Unity">Next <span class="icon icon-circle-arrow-right"></span></a>
      
      
        <a href="../gameobject-execution-order/" class="btn btn-neutral" title="GameObject Execution Order"><span class="icon icon-circle-arrow-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <!-- Copyright etc -->
    
  </div>

  Built with <a href="http://www.mkdocs.org">MkDocs</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
      
        </div>
      </div>

    </section>

  </div>

  <div class="rst-versions" role="note" style="cursor: pointer">
    <span class="rst-current-version" data-toggle="rst-current-version">
      
      
        <span><a href="../gameobject-execution-order/" style="color: #fcfcfc;">&laquo; Previous</a></span>
      
      
        <span style="margin-left: 15px"><a href="../threading-in-unity/" style="color: #fcfcfc">Next &raquo;</a></span>
      
    </span>
</div>
    <script>var base_url = '../..';</script>
    <script src="../../js/theme.js" defer></script>
      <script src="../../search/main.js" defer></script>

</body>
</html>
